package com.firebird.app.fejpsl.ui.activity

import android.content.Intent
import android.view.View

import com.firebird.app.fejpsl.R
import com.firebird.app.fejpsl.base.BaseMvpActivity
import com.firebird.app.fejpsl.constant.Constant
import com.firebird.app.fejpsl.event.LoginEvent
import com.firebird.app.fejpsl.ext.showToast
import com.firebird.app.fejpsl.mvp.contract.LoginContract
import com.firebird.app.fejpsl.mvp.model.bean.LoginData
import com.firebird.app.fejpsl.mvp.presenter.LoginPresenter
import com.firebird.app.fejpsl.utils.DialogUtil
import com.firebird.app.fejpsl.utils.Preference
import kotlinx.android.synthetic.main.toolbar.*
import org.greenrobot.eventbus.EventBus
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseMvpActivity<LoginContract.View, LoginContract.Presenter>(), LoginContract.View {

    /**
     * local username
     */
    private var user: String by Preference(Constant.USERNAME_KEY, "")
    /**
     * local password
     */
    private var pwd: String by Preference(Constant.PASSWORD_KEY, "")
    /**
     * token
     */
    private var token: String by Preference(Constant.TOKEN_KEY, "")
    private var uuid:String by Preference(Constant.UUID_KEY, "")
    private var  nicename:String by Preference(Constant.NICE_NAME_KEY, "")
    private var realname:String by Preference(Constant.REAL_NAME_KEY, "")
    private var  sex:Int by Preference(Constant.SEX_KEY, 1)
    private var  birthday:String by Preference(Constant.BIRTHDAY_KEY, "")
    private var  phone:String by Preference(Constant.PHONE_KEY, "")
    private var  avatar:String by Preference(Constant.AVATAR_KEY, "")
    private var  userid:String by Preference(Constant.USER_ID_KEY, "")
    private var  email:String by Preference(Constant.EMAIL_KEY, "")
    private var  creditscore:Int by Preference(Constant.CREDIT_SCORE_KEY, 100)
    private var  area:String by Preference(Constant.AREA_KEY, "")
    private var  address:String by Preference(Constant.ADDRESS_KEY, "")
    private var  isexplicit:Int by Preference(Constant.IS_EXPLICIT_KEY, 1)
    private var  coppercash:Long by Preference(Constant.COPPER_CASH_KEY, 0)
    private var  goldingot:Long by Preference(Constant.GOLD_INGOT_KEY, 0)
    private var  levelscore:Long by Preference(Constant.LEVEL_SCORE_KEY, 0)
    override fun createPresenter(): LoginContract.Presenter = LoginPresenter()

    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this, getString(R.string.login_ing))
    }

    override fun showLoading() {
        mDialog.show()
    }

    override fun hideLoading() {
        mDialog.dismiss()
    }

    override fun attachLayoutRes(): Int = R.layout.activity_login

    override fun useEventBus(): Boolean = false

    override fun enableNetworkTip(): Boolean = false

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        et_username.setText(user)
        toolbar.run {
            title = resources.getString(R.string.login)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        btn_login.setOnClickListener(onClickListener)
        tv_sign_up.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    override fun loginSuccess(data: LoginData) {
        val myuuid=data.my_uuid
        if (myuuid.length>5) {
            isLogin = true
            user = data.user_name
            //pwd = data.password
            token = data.token
            uuid = data.my_uuid
            nicename = data.nice_name
            realname = data.real_name
            sex = data.sex
            birthday = data.birth_day
            phone = data.phone
            avatar = data.avatar_text
            userid = data.user_id
            email = data.email
            creditscore = data.credit_score
            area = data.area
            address = data.address
            isexplicit = data.is_explicit
            coppercash = data.copper_cash
            goldingot = data.gold_ingot
            levelscore = data.level_score
            EventBus.getDefault().post(LoginEvent(true))
            finish()
        }else{
            showToast(getString(R.string.login_fail))
        }
    }

    override fun loginFail() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_login -> {
                login()
            }
            R.id.tv_sign_up -> {
                val intent = Intent(this@LoginActivity, RegisterActivity::class.java)
                startActivity(intent)
                finish()
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }
        }
    }

    /**
     * Login
     */
    private fun login() {
        if (validate()) {
            mPresenter?.loginFejpslAndroid(et_username.text.toString(), et_password.text.toString())
        }
    }

    /**
     * Check UserName and PassWord
     */
    private fun validate(): Boolean {
        var valid = true
        val username: String = et_username.text.toString()
        val password: String = et_password.text.toString()

        if (username.isEmpty()) {
            et_username.error = getString(R.string.username_not_empty)
            valid = false
        }
        if (password.isEmpty()) {
            et_password.error = getString(R.string.password_not_empty)
            valid = false
        }
        return valid

    }

}
